#include "widget.h"
#include "ui_widget.h"
#include <QLabel>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QLabel* label = new QLabel(ui->tab);
    label->setText("标签页1");

    QLabel* label_2 = new QLabel(ui->tab_2);
    label_2->setText("标签页2");

    //默认第一个定位标签页
    ui->tabWidget->setCurrentIndex(0);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    //获取当前有几个标签页了
    int count = ui->tabWidget->count();
    //创建新的widget
    QWidget* w = new QWidget();
    ui->tabWidget->addTab(w, QString("Tab ") + QString::number(count + 1));
    //给widget中添加label
    QLabel* label = new QLabel(w);
    label->setText(QString("标签页") + QString::number(count + 1));

    //选中这个新的标签页
    ui->tabWidget->setCurrentIndex(count);

}

void Widget::on_pushButton_2_clicked()
{
    //获取当前标签页的下标
    int index = ui->tabWidget->currentIndex();
    //删除这个标签页
    ui->tabWidget->removeTab(index);
}

void Widget::on_tabWidget_currentChanged(int index)
{
    qDebug() << "当前选中标签页为：" << index;
}
